<template>
    <div>
        <el-form label-position="left" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="text-align: left">
            <el-form-item label="套餐名" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="图片" prop="comboPic">
                <el-upload
                        class="avatar-uploader"
                        action="http://localhost:8081/common/uploadPicture"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="默认价格" prop="defaultMoney">
                <el-input v-model="ruleForm.defaultMoney.number"></el-input>
            </el-form-item>
            <el-form-item label="折扣价格"  prop="money">
                <el-input v-model="ruleForm.money.number"></el-input>
            </el-form-item>
            <el-form-item label="食物" prop="foodIdList">
                <el-transfer v-model="ruleForm.foodIdList" :data="data"></el-transfer>
            </el-form-item>
            <el-form-item label="排序"  prop="sort">
                <el-input v-model="ruleForm.sort"></el-input>
            </el-form-item>
            <el-form-item label="影院" prop="cinemaName">
                <el-select v-model="ruleForm.cinemaId" filterable placeholder="请选择">
                    <el-option
                            v-for="item in cinemaData"
                            :key="item.id"
                            :label="item.cinemaName"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "FoodAddView",
        data() {
            const foodListData = _ => {
                const data=[];
                this.axios.get("manage/food/list").then(response=>{
                    var foods=response.data.data.list;
                    foods.forEach((food) => {
                        data.push({
                            label: food.name,
                            key: food.id
                        });
                    });
                })
                return data;
            };
            const cinemaNameData = _ =>{
                const data = [];
                this.axios.get("manage/food/getfood").then(response=>{
                    var cinemaNames=response.data.data;
                    cinemaNames.forEach((cinemaName) => {
                        data.push(
                            cinemaName
                        );
                    });
                })
                return data;
            };
            return{
                ruleForm: {},
                imageUrl: void 0,
                rules: {
                    name: [
                        { required: true, message: '请输入套餐名称', trigger: 'blur' },
                    ],
                    comboPic: [
                        { required: true, message: '请上传图片', trigger: 'blur' },
                    ],
                    defaultMoney: [
                        { required: true, message: '请输入默认价格', trigger: 'blur' },
                        { required: true, type:"number", message: '请输入数字', trigger: 'blur' }
                    ],
                    money: [
                        { required: true, message: '请输入折扣价格', trigger: 'blur' },
                        { required: true, type:"number", message: '请输入数字', trigger: 'blur' }
                    ],
                    sort: [
                        { required :true, message: '请输入排序', trigger: 'blur'}
                    ]
                },
                data:foodListData(),
                cinemaData: cinemaNameData()
            }

        },
        created() {
            if (this.$route.query.data){
                this.ruleForm=this.$route.query.data
            }
            this.ruleForm.foodIdList=this.handleFoodIdListByComboId()
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.loading = true;
                    this.axios.post("manage/combo/add", this.ruleForm).then(response => {
                        if (response.data.code == 200) {
                            this.$router.push({path: "/food/combo"});
                        }
                    })
                }
                    else {
                        this.loading = false;
                        return false;}
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            handleFoodIdListByComboId(){
              console.log(this.ruleForm.id)
                this.axios.get("/manage/combo/findFoodListByComboId",{params:{id: this.ruleForm.id}}).then(response=>{
                    if (response.data.code==200) {
                        this.ruleForm.foodIdList=response.data.data
                    }
                })
            },
            handleAvatarSuccess(res,file) {
                this.ruleForm.comboPic = res.msg;
                this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>

<style scoped>

</style>